package com.szly.phm.admin.time;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.szly.phm.common.entity.po.*;
import com.szly.phm.common.entity.vo.ra.RaRadarRealTimeVO;
import com.szly.phm.common.mapper.*;
import com.szly.phm.common.utils.HandleUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Component
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class BigScreen {
    private Integer limitC=1;
    private Integer limitS=1;
    private final CbandRealdataMapper cbandRealdataMapper;
    private  final RaRadarMapper raRadarMapper;
    private final FiRealTimeFileStructureMapper fiRealTimeFileStructureMapper;
    private final FiSubsystemStatusDataMapper fiSubsystemStatusDataMapper;
    private final TimedTaskFaultResultMapper timedTaskFaultResultMapper;
    private final FaFaultRecordMapper faFaultRecordMapper;
    private final RaTreeMapper raTreeMapper;
    private final HealthScoreMapper healthScoreMapper;
    private final  RaSystemMapper raSystemMapper;
    private final SysMessageMapper sysMessageMapper;
    private final SysParameterMapper sysParameterMapper;
    /**
     *模拟数据
     *
     * @author chengjiang
     * @date 2021/9/15
     */
    @Scheduled(cron = "0 */6 * * * ?", zone = "Asia/Shanghai")
    @Async("scheduleExecutor")
    public void TimingFalseData() {
        String strDateFormat = "yyyyMMddHHmmss";
        SimpleDateFormat df = new SimpleDateFormat(strDateFormat);
        df.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
//        Map<String,Object> map=cbandRealdataMapper.selectRealData(limit);
//        Map<String,Object> map1=cbandRealdataMapper.selectRealDataZ9796(limit);
//        if(map!=null){
//            map.put("uptime",df.format(new Date()));
//            map.put("uuid", HandleUtils.UUID32());
//            cbandRealdataMapper.addFalseData(map,"Z9852_cband_data");
//        }else {
//            limit=1;
        raRadarMapper.selectRaRadarDataTable().forEach(r->{
            if(r.getDataTableName().contains("cband_data")){
                if(limitC>13873){
                    limitC=1;
                }
                Map<String,Object> map2=cbandRealdataMapper.selectCRealData(limitC);
                map2.put("uptime",df.format(new Date()));
                map2.put("uuid", HandleUtils.UUID32());
                cbandRealdataMapper.addFalseData(map2,r.getDataTableName());
                limitC++;
                NonSwitchingValue();
            }
            if(r.getDataTableName().contains("sband_data")){
                if(limitS>29011){
                    limitS=1;
                }
                Map<String,Object> map2=cbandRealdataMapper.selectSRealData(limitS);
                map2.put("uptime",df.format(new Date()));
                map2.put("uuid", HandleUtils.UUID32());
                cbandRealdataMapper.addFalseData(map2,r.getDataTableName());
                limitS++;
                NonSwitchingValue();
            }
        });

//        }
//        if(map1!=null){
//            map.put("uptime",df.format(new Date()));
//            map.put("uuid", HandleUtils.UUID32());
//            cbandRealdataMapper.addFalseDataZ9796(map,"z9796_sband_data");
//        }else {
//            limit=1;
//            Map<String,Object> map3=cbandRealdataMapper.selectSRealData(limit);
//            map3.put("uptime",df.format(new Date()));
//            map3.put("uuid", HandleUtils.UUID32());
//            cbandRealdataMapper.addFalseDataZ9796(map3,"Z9796_sband_data");
//        }
    }
    /**
     *模拟数据
     *
     * @author chengjiang
     * @date 2021/9/15
     */
//    @Scheduled(cron = "0 35 17 ? * *", zone = "Asia/Shanghai")
//    @Scheduled(cron = "0 */6 * * * ?", zone = "Asia/Shanghai")
//    @Async("scheduleExecutor")
    public void NonSwitchingValue() {
        var map1s=sysParameterMapper.selectHealthParameter();
        float poorThreshold= 1.05f;
        float faultThreshold=1.1f;
        for(Map<String,Object> map1:map1s){
            if(map1.get("poorThreshold")!=null&&map1.get("faultThreshold")!=null){
                poorThreshold= 1+Float.parseFloat(map1.get("poorThreshold").toString());
                faultThreshold=1+ Float.parseFloat(map1.get("faultThreshold").toString());
            }
        }
        var date =new Date();
        //遍历所有生成表的雷达
        for(RaRadarRealTimeVO r: raRadarMapper.selectRaRadarFileType()){
                    Map<String, Map<String, Integer>> map2 = new HashMap<>();
                    //查询实时数据
                    Map<String, Object> map = cbandRealdataMapper.selectRealField1(r.getDataTableName());
                    //遍历实时数据里面的参数
                    for (Map.Entry<String, Object> entry : map.entrySet()) {
                        String regex = ".*[a-zA-Z]+.*";
                        Matcher m = Pattern.compile(regex).matcher(entry.getValue().toString());
                        if (!"".equals(entry.getValue()) && !m.matches()) {
                            //            try {
                            //查询实时文件结构里面的数据，有的话代表着实时数据不是状态数据
                            var wrapper = new LambdaQueryWrapper<FiRealTimeFileStructure>();
                            wrapper.eq(FiRealTimeFileStructure::getEName, entry.getKey());
                            wrapper.eq(FiRealTimeFileStructure::getFileTypeId, r.getFileTypeId());
                            wrapper.isNotNull(FiRealTimeFileStructure::getMax);
                            wrapper.isNotNull(FiRealTimeFileStructure::getMin);
                            wrapper.eq(FiRealTimeFileStructure::getIsSoftDelete, false);
                            var realTimeFileStructures = fiRealTimeFileStructureMapper.selectList(wrapper);
                            if (realTimeFileStructures.size() != 0 && Double.valueOf(realTimeFileStructures.get(0).getMax()) > Double.valueOf(realTimeFileStructures.get(0).getMin())) {
                                List<String> ids = new ArrayList<>();
                                var wrapper2 = new LambdaQueryWrapper<RaSystem>();
                                wrapper2.eq(RaSystem::getIsSoftDelete,0);
                                wrapper2.eq(RaSystem::getId,  realTimeFileStructures.get(0).getSystemId());
                                wrapper2.ne(RaSystem::getId,"9");
                                wrapper2.ne(RaSystem::getId,"7");
                               var raSystem= raSystemMapper.selectOne(wrapper2);
                                if(raSystem!=null){
                                    if (Double.valueOf(realTimeFileStructures.get(0).getMax()) >0&&Double.valueOf(entry.getValue().toString()) > Double.valueOf(realTimeFileStructures.get(0).getMax()) * poorThreshold && Float.valueOf(entry.getValue().toString()) < Double.valueOf(realTimeFileStructures.get(0).getMax()) * faultThreshold)
                                    {
                                        if (map2.containsKey(realTimeFileStructures.get(0).getSystemId())) {
                                            map2.get(realTimeFileStructures.get(0).getSystemId()).put("noSwitch", map2.get(realTimeFileStructures.get(0).getSystemId()).get("noSwitch") + 1);
                                            map2.get(realTimeFileStructures.get(0).getSystemId()).put("nonSwitchLow", map2.get(realTimeFileStructures.get(0).getSystemId()).get("nonSwitchLow") + 1);
                                        } else {
                                            Map<String, Integer> map3 = new HashMap<>();
                                            map3.put("switch1", 0);
                                            map3.put("noSwitch", 0);
                                            map3.put("nonSwitchLow", 0);
                                            map3.put("nonSwitchFault", 0);
                                            map3.put("switchFault", 0);
                                            map3.put("nonSwitchLow", map3.get("nonSwitchLow") + 1);
                                            map3.put("noSwitch", map3.get("noSwitch") + 1);
                                            map2.put(realTimeFileStructures.get(0).getSystemId(), map3);
                                        }
                                        insertFaultRecord(r.getName(),raSystem.getName(),ids, r, date, realTimeFileStructures, "超出正常工作范围");

                                    } else if (Double.valueOf(realTimeFileStructures.get(0).getMin()) >0&&Double.valueOf(entry.getValue().toString()) < Double.valueOf(realTimeFileStructures.get(0).getMin()) * (2-poorThreshold) && Float.valueOf(entry.getValue().toString()) < Double.valueOf(realTimeFileStructures.get(0).getMin()) * (2-faultThreshold))
                                    {
                                        if (map2.containsKey(realTimeFileStructures.get(0).getSystemId())) {
                                            map2.get(realTimeFileStructures.get(0).getSystemId()).put("noSwitch", map2.get(realTimeFileStructures.get(0).getSystemId()).get("noSwitch") + 1);
                                            map2.get(realTimeFileStructures.get(0).getSystemId()).put("nonSwitchLow", map2.get(realTimeFileStructures.get(0).getSystemId()).get("nonSwitchLow") + 1);
                                        } else {
                                            Map<String, Integer> map3 = new HashMap<>();
                                            map3.put("switch1", 0);
                                            map3.put("noSwitch", 0);
                                            map3.put("nonSwitchLow", 0);
                                            map3.put("nonSwitchFault", 0);
                                            map3.put("switchFault", 0);
                                            map3.put("nonSwitchLow", map3.get("nonSwitchLow") + 1);
                                            map3.put("noSwitch", map3.get("noSwitch") + 1);
                                            map2.put(realTimeFileStructures.get(0).getSystemId(), map3);
                                        }
                                        insertFaultRecord(r.getName(),raSystem.getName(),ids, r, date, realTimeFileStructures, "超出正常工作范围");
                                    } else if (Double.valueOf(realTimeFileStructures.get(0).getMax()) >0&&Double.valueOf(entry.getValue().toString()) > Double.valueOf(realTimeFileStructures.get(0).getMax()) * faultThreshold)
                                    {
                                        if (map2.containsKey(realTimeFileStructures.get(0).getSystemId()))
                                        {
                                            map2.get(realTimeFileStructures.get(0).getSystemId()).put("noSwitch", map2.get(realTimeFileStructures.get(0).getSystemId()).get("noSwitch") + 1);
                                            map2.get(realTimeFileStructures.get(0).getSystemId()).put("nonSwitchFault", map2.get(realTimeFileStructures.get(0).getSystemId()).get("nonSwitchFault") + 1);
                                        } else {
                                            Map<String, Integer> map3 = new HashMap<>();
                                            map3.put("switch1", 0);
                                            map3.put("noSwitch", 0);
                                            map3.put("nonSwitchLow", 0);
                                            map3.put("nonSwitchFault", 0);
                                            map3.put("switchFault", 0);
                                            map3.put("nonSwitchFault", map3.get("nonSwitchFault") + 1);
                                            map3.put("noSwitch", map3.get("noSwitch") + 1);
                                            map2.put(realTimeFileStructures.get(0).getSystemId(), map3);
                                        }
                                        insertFaultRecord(r.getName(),raSystem.getName(),ids, r, date, realTimeFileStructures, "超出正常工作范围");
                                    } else if (Double.valueOf(realTimeFileStructures.get(0).getMin()) >0&&Double.valueOf(entry.getValue().toString()) < Double.valueOf(realTimeFileStructures.get(0).getMin()) * (2-faultThreshold))
                                    {
                                        if (map2.containsKey(realTimeFileStructures.get(0).getSystemId())) {
                                            map2.get(realTimeFileStructures.get(0).getSystemId()).put("noSwitch", map2.get(realTimeFileStructures.get(0).getSystemId()).get("noSwitch") + 1);
                                            map2.get(realTimeFileStructures.get(0).getSystemId()).put("nonSwitchFault", map2.get(realTimeFileStructures.get(0).getSystemId()).get("nonSwitchFault") + 1);
                                        } else {
                                            Map<String, Integer> map3 = new HashMap<>();
                                            map3.put("switch1", 0);
                                            map3.put("noSwitch", 0);
                                            map3.put("nonSwitchLow", 0);
                                            map3.put("nonSwitchFault", 0);
                                            map3.put("switchFault", 0);
                                            map3.put("nonSwitchFault", map3.get("nonSwitchFault") + 1);
                                            map3.put("noSwitch", map3.get("noSwitch") + 1);
                                            map2.put(realTimeFileStructures.get(0).getSystemId(), map3);
                                        }
                                        insertFaultRecord(r.getName(),raSystem.getName(),ids, r, date, realTimeFileStructures, "超出正常工作范围");
                                    }
                                    else if (Double.valueOf(realTimeFileStructures.get(0).getMax()) <0&&Double.valueOf(entry.getValue().toString()) > Double.valueOf(realTimeFileStructures.get(0).getMax()) * (2-poorThreshold) && Float.valueOf(entry.getValue().toString()) < Double.valueOf(realTimeFileStructures.get(0).getMax()) * (2-faultThreshold))
                                    {
                                        if (map2.containsKey(realTimeFileStructures.get(0).getSystemId())) {
                                            map2.get(realTimeFileStructures.get(0).getSystemId()).put("noSwitch", map2.get(realTimeFileStructures.get(0).getSystemId()).get("noSwitch") + 1);
                                            map2.get(realTimeFileStructures.get(0).getSystemId()).put("nonSwitchLow", map2.get(realTimeFileStructures.get(0).getSystemId()).get("nonSwitchLow") + 1);
                                        } else {
                                            Map<String, Integer> map3 = new HashMap<>();
                                            map3.put("switch1", 0);
                                            map3.put("noSwitch", 0);
                                            map3.put("nonSwitchLow", 0);
                                            map3.put("nonSwitchFault", 0);
                                            map3.put("switchFault", 0);
                                            map3.put("nonSwitchLow", map3.get("nonSwitchLow") + 1);
                                            map3.put("noSwitch", map3.get("noSwitch") + 1);
                                            map2.put(realTimeFileStructures.get(0).getSystemId(), map3);
                                        }
                                        insertFaultRecord(r.getName(),raSystem.getName(),ids, r, date, realTimeFileStructures, "超出正常工作范围");

                                    } else if (Double.valueOf(realTimeFileStructures.get(0).getMin()) <0&&Double.valueOf(entry.getValue().toString()) < Double.valueOf(realTimeFileStructures.get(0).getMin()) * poorThreshold && Float.valueOf(entry.getValue().toString()) < Double.valueOf(realTimeFileStructures.get(0).getMin()) *faultThreshold)
                                    {
                                        if (map2.containsKey(realTimeFileStructures.get(0).getSystemId())) {
                                            map2.get(realTimeFileStructures.get(0).getSystemId()).put("noSwitch", map2.get(realTimeFileStructures.get(0).getSystemId()).get("noSwitch") + 1);
                                            map2.get(realTimeFileStructures.get(0).getSystemId()).put("nonSwitchLow", map2.get(realTimeFileStructures.get(0).getSystemId()).get("nonSwitchLow") + 1);
                                        } else {
                                            Map<String, Integer> map3 = new HashMap<>();
                                            map3.put("switch1", 0);
                                            map3.put("noSwitch", 0);
                                            map3.put("nonSwitchLow", 0);
                                            map3.put("nonSwitchFault", 0);
                                            map3.put("switchFault", 0);
                                            map3.put("nonSwitchLow", map3.get("nonSwitchLow") + 1);
                                            map3.put("noSwitch", map3.get("noSwitch") + 1);
                                            map2.put(realTimeFileStructures.get(0).getSystemId(), map3);
                                        }
                                        insertFaultRecord(r.getName(),raSystem.getName(),ids, r, date, realTimeFileStructures, "超出正常工作范围");
                                    } else if (Double.valueOf(realTimeFileStructures.get(0).getMax()) <0&&Double.valueOf(entry.getValue().toString()) > Double.valueOf(realTimeFileStructures.get(0).getMax()) * (2-faultThreshold))
                                    {
                                        if (map2.containsKey(realTimeFileStructures.get(0).getSystemId()))
                                        {
                                            map2.get(realTimeFileStructures.get(0).getSystemId()).put("noSwitch", map2.get(realTimeFileStructures.get(0).getSystemId()).get("noSwitch") + 1);
                                            map2.get(realTimeFileStructures.get(0).getSystemId()).put("nonSwitchFault", map2.get(realTimeFileStructures.get(0).getSystemId()).get("nonSwitchFault") + 1);
                                        } else {
                                            Map<String, Integer> map3 = new HashMap<>();
                                            map3.put("switch1", 0);
                                            map3.put("noSwitch", 0);
                                            map3.put("nonSwitchLow", 0);
                                            map3.put("nonSwitchFault", 0);
                                            map3.put("switchFault", 0);
                                            map3.put("nonSwitchFault", map3.get("nonSwitchFault") + 1);
                                            map3.put("noSwitch", map3.get("noSwitch") + 1);
                                            map2.put(realTimeFileStructures.get(0).getSystemId(), map3);
                                        }
                                        insertFaultRecord(r.getName(),raSystem.getName(),ids, r, date, realTimeFileStructures, "超出正常工作范围");
                                    } else if (Double.valueOf(realTimeFileStructures.get(0).getMin()) <0&&Double.valueOf(entry.getValue().toString()) < Double.valueOf(realTimeFileStructures.get(0).getMin()) * faultThreshold)
                                    {
                                        if (map2.containsKey(realTimeFileStructures.get(0).getSystemId())) {
                                            map2.get(realTimeFileStructures.get(0).getSystemId()).put("noSwitch", map2.get(realTimeFileStructures.get(0).getSystemId()).get("noSwitch") + 1);
                                            map2.get(realTimeFileStructures.get(0).getSystemId()).put("nonSwitchFault", map2.get(realTimeFileStructures.get(0).getSystemId()).get("nonSwitchFault") + 1);
                                        } else {
                                            Map<String, Integer> map3 = new HashMap<>();
                                            map3.put("switch1", 0);
                                            map3.put("noSwitch", 0);
                                            map3.put("nonSwitchLow", 0);
                                            map3.put("nonSwitchFault", 0);
                                            map3.put("switchFault", 0);
                                            map3.put("nonSwitchFault", map3.get("nonSwitchFault") + 1);
                                            map3.put("noSwitch", map3.get("noSwitch") + 1);
                                            map2.put(realTimeFileStructures.get(0).getSystemId(), map3);
                                        }
                                        insertFaultRecord(r.getName(),raSystem.getName(),ids, r, date, realTimeFileStructures, "超出正常工作范围");
                                    }
                                    else {
                                        if (map2.containsKey(realTimeFileStructures.get(0).getSystemId()))
                                        {
                                            map2.get(realTimeFileStructures.get(0).getSystemId()).put("noSwitch", map2.get(realTimeFileStructures.get(0).getSystemId()).get("noSwitch") + 1);
                                        } else {
                                            Map<String, Integer> map3 = new HashMap<>();
                                            map3.put("switch1", 0);
                                            map3.put("noSwitch", 0);
                                            map3.put("nonSwitchLow", 0);
                                            map3.put("nonSwitchFault", 0);
                                            map3.put("switchFault", 0);
                                            map3.put("nonSwitchFault", 0);
                                            map3.put("noSwitch", map3.get("noSwitch") + 1);
                                            map2.put(realTimeFileStructures.get(0).getSystemId(), map3);
                                        }
                                    }
                                }

                            }
                            //查询状态数据
                            var wrapper1 = new LambdaQueryWrapper<FiSubsystemStatusData>();
                            wrapper1.eq(FiSubsystemStatusData::getEName, entry.getKey());
                            wrapper1.eq(FiSubsystemStatusData::getFileTypeId, r.getFileTypeId());
                            wrapper1.eq(FiSubsystemStatusData::getIsSoftDelete, false);
                            var subsystemStatusDatas = fiSubsystemStatusDataMapper.selectList(wrapper1);
                            if (subsystemStatusDatas.size() != 0) {
                                List<String> ids = new ArrayList<>();
                                var wrapper2 = new LambdaQueryWrapper<RaSystem>();
                                wrapper2.eq(RaSystem::getIsSoftDelete,0);
                                wrapper2.eq(RaSystem::getId,  subsystemStatusDatas.get(0).getSubsystemId());
                                wrapper2.ne(RaSystem::getId,"9");
                                wrapper2.ne(RaSystem::getId,"7");
                                var raSystem=raSystemMapper.selectOne(wrapper2);
                             if(raSystem!=null){
                                 if (map2.containsKey(subsystemStatusDatas.get(0).getSubsystemId())) {
                                     map2.get(subsystemStatusDatas.get(0).getSubsystemId()).put("switch1", map2.get(subsystemStatusDatas.get(0).getSubsystemId()).get("switch1") + 1);
                                     if ("0".equals(subsystemStatusDatas.get(0).getZeroStatusMean()) && "0".equals(entry.getValue())) {
                                         map2.get(subsystemStatusDatas.get(0).getSubsystemId()).put("switchFault", map2.get(subsystemStatusDatas.get(0).getSubsystemId()).get("switchFault") + 1);
                                         insertFaultRecord(r.getName(),raSystem.getName(),ids, r, date, realTimeFileStructures, "故障");
                                     }
                                 } else {
                                     Map<String, Integer> map3 = new HashMap<>();
                                     map3.put("switch1", 0);
                                     map3.put("noSwitch", 0);
                                     map3.put("nonSwitchLow", 0);
                                     map3.put("nonSwitchFault", 0);
                                     map3.put("switchFault", 0);
                                     map3.put("switch1", map3.get("switch1") + 1);
                                     if ("0".equals(subsystemStatusDatas.get(0).getZeroStatusMean()) && "0".equals(entry.getValue())) {
                                         map3.put("switchFault", map3.get("switchFault") + 1);
                                         insertFaultRecord(r.getName(),raSystem.getName(),ids, r, date, realTimeFileStructures, "故障");
                                     }
                                     map2.put(subsystemStatusDatas.get(0).getSubsystemId(), map3);
                                 }
                             }
                            }
//            } catch (Exception e) {
//
//            }
                        }
                    }
                    map2.forEach((k, v) -> {
                        var timedTaskFaultResult = new TimedTaskFaultResult();
                        timedTaskFaultResult.setId(HandleUtils.UUID32());
                        timedTaskFaultResult.setRadarId(r.getRadarId());
                        timedTaskFaultResult.setIsSoftDelete(false);
                        timedTaskFaultResult.setCreator("定时任务生成");
                        timedTaskFaultResult.setCreateDateTime(date);
                        timedTaskFaultResult.setRadarSystemId(k);
                        timedTaskFaultResult.setNoSwitch(v.get("noSwitch"));
                        timedTaskFaultResult.setSwitch1(v.get("switch1"));
                        timedTaskFaultResult.setSwitchFault(v.get("switchFault"));
                        timedTaskFaultResult.setNonSwitchLow(v.get("nonSwitchLow"));
                        timedTaskFaultResult.setNonSwitchFault(v.get("nonSwitchFault"));
                        timedTaskFaultResultMapper.insert(timedTaskFaultResult);
                    });

                }
        calculationScore();
    }

    void calculationScore(){
        raRadarMapper.selectRaRadarFileType().forEach(r->{
            var timedTaskFaultResults= timedTaskFaultResultMapper.selectTimedTaskFaultResultNow(r.getRadarId());
            float weight=0;
            double score=0;
            List<Float> wight=new ArrayList<>();
            List<Double> systemScore=new ArrayList<>();
            for(var t:timedTaskFaultResults){
                var wrapper = new LambdaQueryWrapper<RaSystem>();
                wrapper.eq(RaSystem::getIsSoftDelete,0);
                wrapper.eq(RaSystem::getId,t.getRadarSystemId());
                var raSystem=raSystemMapper.selectOne(wrapper);
                if(raSystem!=null){
                  var w=  raSystemMapper.selectOne(wrapper).getWeight();
                    weight+=w;
                    wight.add(w);
                }else {
                    weight+=0;
                }

            }
            var date=new Date();
            for(var t:timedTaskFaultResults){
                if(t.getSwitch1()+t.getNoSwitch()!=0){
                    double fenZi= t.getNonSwitchLow();
                    double fenMu= t.getSwitch1()+t.getNoSwitch();
                    var Score= ((double)1-(fenZi/fenMu))*100;
                    systemScore.add(Score);
                }else {
                    systemScore.add(0.0);
                }

            }
            //计算雷达分数
            int i=0;
            for(float f:wight){
                score+=  systemScore.get(i)*(f/weight);
                i++;
            }
            for(var t:timedTaskFaultResults){
                if(t.getSwitch1()+t.getNoSwitch()!=0){
                    var healthScore=new HealthScore();
                    healthScore.setId(HandleUtils.UUID32());
                    healthScore.setCreateDateTime(date);
                    healthScore.setCreator("系统生成");
                    healthScore.setSubsystemId(t.getRadarSystemId());
                    healthScore.setRadarId(t.getRadarId());
                    double fenZi= t.getNonSwitchLow();
                    double fenMu= t.getSwitch1()+t.getNoSwitch();
                   var Score= (1-(fenZi/fenMu))*100;
                    healthScore.setSubsystemScore(Score);
                    var wrapper = new LambdaQueryWrapper<RaSystem>();
                    wrapper.eq(RaSystem::getIsSoftDelete,0);
                    wrapper.eq(RaSystem::getId,t.getRadarSystemId());
                   var raSystem= raSystemMapper.selectOne(wrapper);
                    if(raSystem!=null&&weight!=0){
                            healthScore.setSubsystemWeightCoefficient(raSystemMapper.selectOne(wrapper).getWeight()/weight);
                    }else {
                        healthScore.setSubsystemWeightCoefficient(0f);
                        System.out.println(t.getRadarSystemId());
                    }
                    healthScore.setRadarScore(new BigDecimal((float)score).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                    healthScoreMapper.insert(healthScore);
                }

            }
        });

    }

    String selectStructureId(List<String> ids){
        var id="";
        if(ids.size()==1){
            var wrapper = new LambdaQueryWrapper<RaTree>();
            wrapper.eq(RaTree::getIsSoftDelete,0);
            wrapper.eq(RaTree::getRadarId,ids.get(0));
            wrapper.isNull(RaTree::getSystemId);
            wrapper.isNull(RaTree::getExtensionId);
            wrapper.isNull(RaTree::getKeyPartsModelId);
            var raTreeList=raTreeMapper.selectList(wrapper);
            if(raTreeList.size()!=0){
                id+= raTreeList.get(0).getId();
            }

        }
        if(ids.size()==2){
            var wrapper = new LambdaQueryWrapper<RaTree>();
            wrapper.eq(RaTree::getIsSoftDelete,0);
            wrapper.eq(RaTree::getRadarId,ids.get(0));
            wrapper.eq(RaTree::getSystemId,ids.get(1));
            wrapper.isNull(RaTree::getExtensionId);
            wrapper.isNull(RaTree::getKeyPartsModelId);
           var raTree= raTreeMapper.selectList(wrapper);
            if(raTree.size()!=0)
            {
                id+= raTree.get(0).getId();
            }
        }
        if(ids.size()==3){
            var wrapper = new LambdaQueryWrapper<RaTree>();
            wrapper.eq(RaTree::getIsSoftDelete,0);
            wrapper.eq(RaTree::getRadarId,ids.get(0));
            wrapper.eq(RaTree::getSystemId,ids.get(1));
            wrapper.eq(RaTree::getExtensionId,ids.get(2));
            wrapper.isNull(RaTree::getKeyPartsModelId);
            var raTree= raTreeMapper.selectList(wrapper);
            if(raTree.size()!=0)
            {
                id+= raTree.get(0).getId();
            }
        }
        if(ids.size()==4){
            var wrapper = new LambdaQueryWrapper<RaTree>();
            wrapper.eq(RaTree::getIsSoftDelete,0);
            wrapper.eq(RaTree::getRadarId,ids.get(0));
            wrapper.eq(RaTree::getSystemId,ids.get(1));
            wrapper.eq(RaTree::getExtensionId,ids.get(2));
            wrapper.eq(RaTree::getKeyPartsModelId,ids.get(3));
            var raTree= raTreeMapper.selectList(wrapper);
            if(raTree.size()!=0)
            {
                id+= raTree.get(0).getId();
            }
        }
return id;
    }

        void insertFaultRecord(String radarName,String raSystemName,List<String> ids, RaRadarRealTimeVO r,Date date,List<FiRealTimeFileStructure> realTimeFileStructures,String s){
            var faFaultRecord = new FaFaultRecord();
            faFaultRecord.setId(HandleUtils.UUID32());
            faFaultRecord.setCreateDateTime(new Date());
            faFaultRecord.setIsSoftDelete(false);
            faFaultRecord.setCreator("定时任务生成");
            faFaultRecord.setDownTime(date);
            faFaultRecord.setIsDeal(0);
            faFaultRecord.setFaultResource(1);
            faFaultRecord.setRemarks(radarName+"-"+raSystemName+"-"+realTimeFileStructures.get(0).getCName() + s);
            if(r.getRadarId()!=null) {
                faFaultRecord.setId(HandleUtils.UUID32());
                ids.add(r.getRadarId());
            }
            if(realTimeFileStructures.get(0).getSystemId()!=null){
                faFaultRecord.setId(HandleUtils.UUID32());
                ids.add(realTimeFileStructures.get(0).getSystemId());
            }
            if(realTimeFileStructures.get(0).getExtensionId()!=null){

                ids.add(realTimeFileStructures.get(0).getExtensionId());
            }
            if(realTimeFileStructures.get(0).getKeyPartsModelId()!=null){
                faFaultRecord.setId(HandleUtils.UUID32());
                ids.add(realTimeFileStructures.get(0).getKeyPartsModelId());
            }
            faFaultRecord.setStructureId(selectStructureId(ids));
            faFaultRecordMapper.insert(faFaultRecord);
            SysMessage sysMessage=new SysMessage();
            sysMessage.setCreateDateTime(new Date());
            sysMessage.setIsSoftDelete(false);
            sysMessage.setCreator("定时任务生成");
            sysMessage.setSource("系统自动判断");
            sysMessage.setType(0);
            sysMessage.setContent(faFaultRecord.getRemarks());
            sysMessageMapper.insert(sysMessage);
        }

}
